﻿Imports Microsoft.Win32
Imports System.Net.Mail

Partial Public Class SettingsWindow

    Public Shared ReadOnly SendMailCommand As RoutedCommand = New RoutedCommand("SendMail", GetType(SettingsWindow))
    Public Shared ReadOnly PlaySoundCommand As RoutedCommand = New RoutedCommand("PlaySound", GetType(SettingsWindow))

    Private ReadOnly Property Context() As UserSettings
        Get
            Return DirectCast(Me.DataContext, UserSettings)
        End Get
    End Property

    Private Sub SendMailCommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)

        Alerter.SendTestMail("テスト送信", "ニコ生アラート（仮）", EmailTextBox.Text, SmtpServerTextBox.Text, CInt(PortTextBox.Text))

    End Sub

    Private Sub SendMailCommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)

        If Me.EmailTextBox.Text <> "" Then
            e.CanExecute = True
        End If

    End Sub

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        BindingGroup.CommitEdit()
        Me.Close()
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub SelectSoundFileButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Dim dialog = New OpenFileDialog With { _
            .Filter = "サウンドファイル(*.wav)|*.wav"}

        If dialog.ShowDialog Then
            Me.Context.SoundFile = dialog.FileName
        End If

    End Sub

    Private Sub PlaySoundCommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        Alerter.TestPlaySound(Me.SoundFileTextBox.Text)
    End Sub

    Private Sub PlaySoundCommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        If Me.SoundFileTextBox.Text <> "" Then
            e.CanExecute = True
        End If
    End Sub

    Private Sub SelectBrowserPathButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dialog = New OpenFileDialog With { _
            .Filter = "Webブラウザアプリケーション(*.exe)|*.exe"}
        If dialog.ShowDialog Then
            Me.Context.WebBrowserPath = dialog.FileName
        End If
    End Sub
End Class
